package org.fast.easy.auth.service.impl;

import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import org.fast.easy.auth.domain.vo.LoginVo;
import org.fast.easy.core.domain.auth.model.LoginUser;
import org.fast.easy.core.domain.auth.model.XcxLoginUser;
import org.fast.easy.framework.exception.user.UserException;
import org.fast.easy.framework.satoken.utils.LoginHelper;
import org.fast.easy.system.client.domain.vo.SysClientVo;

/**
 * 类说明
 *
 * @author YanMing
 * @date 2024/5/29
 */
public abstract class AbstractAuthStrategy {

    protected LoginVo getUserLoginVo(LoginUser loginUser, SysClientVo client){
        LoginVo loginVo = null;
        if(loginUser!=null) {
            loginUser.setClientKey(client.getClientKey());
            loginUser.setDeviceType(client.getDeviceType());
            SaLoginModel model = new SaLoginModel();
            model.setDevice(client.getDeviceType());
            // 自定义分配 不同用户体系 不同 token 授权时间 不设置默认走全局 yml 配置
            // 例如: 后台用户30分钟过期 app用户1天过期
            model.setTimeout(client.getTimeout());
            model.setActiveTimeout(client.getActiveTimeout());
            model.setExtra(LoginHelper.CLIENT_KEY, client.getClientId());
            // 生成token
            LoginHelper.login(loginUser, model);

            loginVo = new LoginVo();
            loginVo.setAccessToken(StpUtil.getTokenValue());
            loginVo.setExpireIn(StpUtil.getTokenTimeout());
            loginVo.setClientId(client.getClientId());
        }else{
            throw new UserException("0",0);
        }
        return loginVo;
    }

    protected LoginVo getUserLoginVo(LoginUser loginUser, SysClientVo client,String openid) {
        LoginVo loginVo = getUserLoginVo(loginUser,client);
        if(loginVo!=null){
            loginVo.setOpenid(openid);
        }
        return loginVo;
    }

}
